`
https://leetcode.cn/problems/minimum-discards-to-balance-inventory/
`

/**
 * @param {number[]} arrivals
 * @param {number} w
 * @param {number} m
 * @return {number}
 */
var minArrivalsToDiscard = function (arrivals, w, m) {
  const n = arrivals.length
  // 应该扔掉的物品数量
  let drop = 0
  const counter = new Map()
  let left = 0, right = 0

  // 维护一个长度为 w 的滑动窗口
  while (right < n) {
    const n1 = arrivals[right]
    const val = counter.get(n1) || 0
    // 已经到达限制 m
    if (val === m) {
      drop++
      // 此时新进入窗口的 n1 被标记为丢弃，待会缩小窗口时应该忽略 n1
      // 题目数据里不会出现 0，将其设置为 0 表示丢弃
      arrivals[right] = 0
    } else {
      counter.set(n1, val + 1)
    }
    right++

    if (right - left >= w) {
      const n2 = arrivals[left++]
      const curCount = counter.get(n2) || 0
      if (curCount === 1) {
        counter.delete(n2)
      } else {
        counter.set(n2, curCount - 1)

      }
    }
  }

  return drop
};